<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/9
 * Time: 10:38
 */

namespace App\Http\Controllers;


class ApiController extends Controller
{
    /*
     * 状态码
     */
    protected $successStatus = 200;

    /*
     * 统一输出错误格式
     */
    protected function outWarning($msg = "", $status = 500)
    {
        return response()->json([
            'msg' => $msg,
            'state' => $status
        ], $status);
    }

    /*
     * 统一输出格式
        {
          "data": {
            "list": null,
            "pagebar": {
              "page": 1,
              "total": 2,
              "limit": 10
            }
          },
          "msg": "error",
          "state": 0,
          "is_redirect": true,
          "redirect_url": "http://qq.com",
          "token": null
        }
    */
    protected function outSuccessData($data = [], $pagebar = [], $msg = "success")
    {
        if(empty($pagebar)){
            $result = [
                'data' => $data,
                'msg' => $msg,
                'state' => $this->successStatus
            ];
        }else{
            $result = [
                'data' => [
                    'list' => $data,
                    "pagebar" => $pagebar
                ],
                'msg' => $msg,
                'state' => $this->successStatus
            ];
        }
        return response()->json($result, $this->successStatus);
    }
}